<template>
  <div class="product-add">
    <el-form size="" ref="form" label-width="100px">
      <el-form-item label="购买商品升级"></el-form-item>
      <el-form-item size="" label="">
        <el-col>
          <div class="common-level-rail">
            <el-button size="" icon="Plus" @click="addClick"
              >添加商品</el-button
            >
          </div>
        </el-col>
        <el-col>
          <div class="pb50">
            <el-table size="" :data="tableData" border>
              <el-table-column label="图片" width="60">
                <template #default="scope">
                  <img :width="30" v-img-url="scope.row.product_image" />
                </template>
              </el-table-column>
              <el-table-column
                prop="product_name"
                label="商品"
              ></el-table-column>
              <el-table-column fixed="right" label="操作" width="90">
                <template #default="scope">
                  <el-button
                    @click="deleteClick(scope.row)"
                    link
                    type="primary"
                    size=""
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>
      </el-form-item>
      <!--提交-->
      <div class="common-button-wrapper">
        <el-button type="primary" size="" @click="onSubmit" :loading="loading"
          >提交</el-button
        >
      </div>
    </el-form>
    <!--选择商品-->
    <Product
      :isproduct="isproduct"
      @closeDialog="closeProductDialogFunc($event)"
      >产品列表弹出层</Product
    >
  </div>
</template>

<script>
import { getGradeProduct,saveGradeProduct } from "@/api/user.js";
import Product from "@/components/product/Product.vue";
export default {
  components: {
    /*商品选择*/
    Product,
  },
  data() {
    return {
      grade_id:0,
      tableData: [],
      isproduct:false,
      product_arr: [],
      Data: [],
      loading: false,
    };
  },
  created() {
    console.log(this.$route.query)
    this.grade_id = this.$route.query.grade_id;
    /*获取数据*/
    this.getData();
  },
  methods: {
    /*获取当前数据*/
    getData() {
      let self = this;
      self.loading = true;
      console.log(this.grade_id)
      getGradeProduct({
        grade_id:this.grade_id,
      }).then((data) => {
          self.loading = false;
          self.tableData = data.data
        })
        .catch((error) => {
          self.loading = false;
        });
    },

    /**
     * 保存数据
     */
    onSubmit() {
      let product_ids = [];
      for (let i = 0; i < this.tableData.length; i++) {
        product_ids.push(this.tableData[i].product_id);
      }
      let param = {
        grade_id: this.grade_id,
        product_ids: product_ids,
      };
      saveGradeProduct(param).then((res)=>{
        ElMessage({
          message: '保存成功',
          type: "success",
        });
      })
    },
    /*关闭弹窗*/
    closeProductDialogFunc(e) {
      this.isproduct = e.openDialog;
      if (e.type == "success") {
        if (this.product_arr.indexOf(e.params.product_id) > -1) {
          ElMessage({
            message: "你已选中该产品",
            type: "error",
          });
          return false;
        }
        let param = {};
        param.product_id = e.params.product_id;
        param.product_name = e.params.product_name;
        param.product_image = e.params.product_image;
        param.sort = 1;
        this.tableData.push(param);
        this.product_arr.push(e.params.product_id);
      }
    },
    /*添加商品*/
    addClick() {
      console.log("here")
      this.isproduct = true;
    },
    /*删除商品*/
    deleteClick(e) {
      this.tableData = this.tableData.filter((item) => {
        return item.product_id != e.product_id;
      });
    },
  },
};
</script>

<style></style>
